<template>
  <div class="app-container">
    <div style="margin: 10px 30px;">总收益 ： {{totalAll}}</div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <!-- <el-form-item label="总收益:" prop="flowPrice">
        <el-input
          v-model="queryParams.flowPrice"
          placeholder="请输入总价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="用户昵称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="支付方式返回的交易号" prop="payOrderNo">
        <el-input
          v-model="queryParams.payOrderNo"
          placeholder="请输入支付方式返回的交易号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="购买时间" prop="paymentTime">
        <el-date-picker clearable
          v-model="queryParams.paymentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购买时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="购买会员等级" prop="memberLevel">
        <el-select v-model="queryParams.memberLevel" clearable placeholder="请选择会员等级">
          <el-option
            v-for="item in memberLevelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.memberLevel"
          placeholder="请输入购买会员等级"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="订单编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yh:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yh:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yh:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yh:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户头像" align="center">
        <template slot-scope="scope" >
          <img v-if="scope.row.face" :src="baseUrl + scope.row.face" alt="" style="width: 100px;height: 100px;">
        </template>
      </el-table-column>
      <el-table-column label="购买会员等级" align="center" prop="memberLevelText" />
      <!-- <el-table-column label="删除标志 true/false 删除/未删除" align="center" prop="deleteFlag" /> -->
      <!-- <el-table-column label="订单取消原因" align="center" prop="cancelReason" /> -->
      <!-- <el-table-column label="完成时间" align="center" prop="completeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="金额" align="center" prop="flowPrice" />
      <!-- <el-table-column label="商品价格" align="center" prop="goodsPrice" /> -->
      <!-- <el-table-column label="会员ID" align="center" prop="memberId" /> -->
      <!-- <el-table-column label="用户名" align="center" prop="memberName" /> -->
      <el-table-column label="订单状态" align="center" prop="orderStatusText" />
      <!-- <el-table-column label="支付方式返回的交易号" align="center" prop="payOrderNo" /> -->
      <el-table-column label="购买时间" align="center" prop="paymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="会员等级id" align="center" prop="memberLevelRuleId" /> -->
      <el-table-column label="订单编号" align="center" prop="sn" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yh:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yh:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员购买订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="删除标志 true/false 删除/未删除" prop="deleteFlag">
          <el-input v-model="form.deleteFlag" placeholder="请输入删除标志 true/false 删除/未删除" />
        </el-form-item>
        <el-form-item label="订单取消原因" prop="cancelReason">
          <el-input v-model="form.cancelReason" placeholder="请输入订单取消原因" />
        </el-form-item>
        <el-form-item label="完成时间" prop="completeTime">
          <el-date-picker clearable
            v-model="form.completeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总价格" prop="flowPrice">
          <el-input v-model="form.flowPrice" placeholder="请输入总价格" />
        </el-form-item>
        <el-form-item label="商品价格" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="会员ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员ID" />
        </el-form-item>
        <el-form-item label="用户名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="支付方式返回的交易号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入支付方式返回的交易号" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="会员等级id" prop="memberLevelRuleId">
          <el-input v-model="form.memberLevelRuleId" placeholder="请输入会员等级id" />
        </el-form-item>
        <el-form-item label="购买会员等级" prop="memberLevel">
          <el-input v-model="form.memberLevel" placeholder="请输入购买会员等级" />
        </el-form-item>
        <el-form-item label="订单编号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入订单编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, totalOrder } from "@/api/yh/order";
import baseUrl from "@/api/baseUrl";
export default {
  name: "Order",
  data() {
    return {
      baseUrl: baseUrl,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员购买订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deleteFlag: null,
        cancelReason: null,
        completeTime: null,
        flowPrice: null,
        goodsPrice: null,
        memberId: null,
        memberName: null,
        orderStatus: null,
        payOrderNo: null,
        paymentTime: null,
        memberLevelRuleId: null,
        memberLevel: null,
        sn: null
      },
      totalAll:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
      },
      memberLevelOptions:[
        {
          value:1,
          label:'Lv-1'
        },
        {
          value:2,
          label:'Lv-2'
        },
        {
          value:3,
          label:'Lv-3'
        },
        {
          value:4,
          label:'Lv-4'
        },
        {
          value:5,
          label:'Lv-5'
        },
      ]
    };
  },
  created() {
    this.getList();
    this.getTotalOrder()
  },
  methods: {
    // 获取总收益
    getTotalOrder(){
      totalOrder().then(response => {
        this.totalAll = response.data;
      });
    },
    /** 查询会员购买订单列表 */
    getList() {
      this.loading = true;
      let arr = ['Lv-1','Lv-2','Lv-3','Lv-4','Lv-5']
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows.map(item =>{
          return{
            ...item,
            memberLevelText:arr[item.memberLevel-1],
            orderStatusText:item.orderStatus == 'PAID'?'已支付':'未支付'
          }
        });
        // this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        deleteFlag: null,
        updateBy: null,
        updateTime: null,
        cancelReason: null,
        completeTime: null,
        flowPrice: null,
        goodsPrice: null,
        memberId: null,
        memberName: null,
        orderStatus: null,
        payOrderNo: null,
        paymentTime: null,
        memberLevelRuleId: null,
        memberLevel: null,
        sn: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员购买订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员购买订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员购买订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
